﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Factory.RBAC.RolePermissions.Dto;
using Factory.MasterData.MaterialClassifications.Dto;
using Factory.MasterData.Materials;

namespace Factory.RBAC.RolePermissions
{
    public class RolePermissionsAppService : ApplicationService, IRolePermissionsAppService
    {
        /// <summary>
        /// 角色仓储接口
        /// </summary>
        private readonly IRepository<Permission> _permissionrepository;
        /// <summary>
        /// 角色权限关联仓储接口
        /// </summary>
        private readonly IRepository<RolePermission> _rolePermissionrepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rolerepository">角色仓储接口</param>
        /// <param name="rolePermissionrepository">构造函数</param>
        public RolePermissionsAppService(IRepository<Permission> permissionrepository, IRepository<RolePermission> rolePermissionrepository)
        {
            _permissionrepository = permissionrepository;
            _rolePermissionrepository = rolePermissionrepository;
        }
        /// <summary>
        /// 查询树形角色权限信息
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <returns>返回结果</returns>
        public async Task<List<RolePermissionsTreeDto>> GetTree(int id)
        {
            var permissions = await _permissionrepository.GetListAsync(m => m.IsDel == 1);
            var rolepermission = await _rolePermissionrepository.GetListAsync(m => m.RoleId == id);
            List<RolePermissionsTreeDto> rolePermissions = GetTreeChildren(rolepermission, permissions, 0);
            return rolePermissions;
        }
        /// <summary>
        /// 查询树形角色权限子集
        /// </summary>
        /// <param name="rolePermissions">角色权限信息</param>
        /// <param name="permissions">权限信息</param>
        /// <param name="pid">父级</param>
        /// <returns>返回结果</returns>
        public List<RolePermissionsTreeDto> GetTreeChildren(List<RolePermission> rolePermissions, List<Permission> permissions, int pid)
        {
            var list = permissions.Where(m => m.Pid == pid).ToList();
            List<RolePermissionsTreeDto> rolePermissionsTreeDtoList = new List<RolePermissionsTreeDto>();
            foreach (var item in list)
            {
                RolePermissionsTreeDto rolePermissionsTreeDto = new RolePermissionsTreeDto();
                rolePermissionsTreeDto.Id = item.Id;
                rolePermissionsTreeDto.title = item.PermissionName;
                rolePermissionsTreeDto.field = item.PermissionName;
                long count = rolePermissions.Count(m => m.PermissionId == item.Id);
                if (count > 0)
                {
                    rolePermissionsTreeDto.Checked = true;
                }
                rolePermissionsTreeDto.children = GetTreeChildren(rolePermissions, permissions, rolePermissionsTreeDto.Id);
                rolePermissionsTreeDtoList.Add(rolePermissionsTreeDto);
            }
            return rolePermissionsTreeDtoList;
        }
        /// <summary>
        /// 添加角色权限数据
        /// </summary>
        /// <param name="rolePermissionsTreeDtos">树形角色权限数据</param>
        /// <param name="id">角色编号</param>
        /// <returns>返回结果</returns>
        public async Task<int> Insert(List<RolePermissionsTreeDto> rolePermissionsTreeDtos, int id)
        {
            List<RolePermission> rolePermissions = GetRolePermission(rolePermissionsTreeDtos, id, new List<RolePermission>());
            List<RolePermission> info = _rolePermissionrepository.GetListAsync(x => x.RoleId == id).Result;
            foreach (var item in info)
            {
                await _rolePermissionrepository.DeleteAsync(m=>m.Id== item.Id);
            }
            foreach (var item in rolePermissions)
            {
                await _rolePermissionrepository.InsertAsync(item);
            }
            return 1;

        }
        /// <summary>
        /// 查询角色权限集合
        /// </summary>
        /// <param name="rolePermissionsTreeDtos">树形角色权限数据</param>
        /// <param name="id">角色编号</param>
        /// <param name="rolePermissions">角色权限集合</param>
        /// <returns>返回结果</returns>
        public List<RolePermission> GetRolePermission(List<RolePermissionsTreeDto> rolePermissionsTreeDtos, int id, List<RolePermission> rolePermissions)
        {
            foreach (var item in rolePermissionsTreeDtos)
            {
                RolePermission rolePermission = new RolePermission();
                rolePermission.PermissionId = item.Id;
                rolePermission.RoleId = id;
                rolePermissions.Add(rolePermission);
                List<RolePermission> rolePermissionList = GetRolePermission(item.children, id, rolePermissions);
            }
            return rolePermissions;
        }
    }
}
